import { login } from '../api/login';

export function Login ({noToast}){
  return new Promise((resolve,reject) => {
    wx.login({
      success: async res => {
        var loginRes = await login(res.code);
        // .then(loginRes => {
          if(loginRes.success){
            wx.setStorageSync('token', loginRes.data.token)
            wx.setStorageSync('realNameStatus', loginRes.data.realNameStatus)
            wx.setStorageSync('mobile', loginRes.data.mobile)
            wx.setStorageSync('userId', loginRes.data.id)
            wx.setStorageSync('avatar', loginRes.data.avatar)
            wx.setStorageSync('nickName', loginRes.data.userName)
            wx.setStorageSync('preUserName', loginRes.data.preUserName)
            wx.setStorageSync('preMobile', loginRes.data.preMobile)
            wx.setStorageSync('realName', loginRes.data.realName)
            wx.setStorageSync('userIdCard', loginRes.data.idCard)
            console.log('用户登录')
          }else{
            console.log(noToast,'noToast')
            if(!noToast){
              wx.showToast({
                title: loginRes.errMessage,
                icon: 'none',
              })
            }
          }
          resolve();
      },
      fail: ()=>{
        resolve();
      },
    })
  })
}